<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="用户ID" width="65px" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" width="65px" align="center"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="75px" align="center"></el-table-column>
      <el-table-column prop="password" label="密码" width="300px" align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号码" width="110px" align="center"></el-table-column>
      <el-table-column prop="enable" label="状态" width="200px" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="updateEnable(scope.row)"
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="permission" label="权限" width="180px" align="center"></el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--以下是弹出的修改用户的对话框-->
    <el-dialog title="修改用户" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm" >
        <el-form-item label="用户ID" :label-width="formLabelWidth" >
          <el-input v-model="ruleForm.id" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.username" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="昵称" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.password" autocomplete="off" ></el-input>
        </el-form-item>
        <el-form-item label="手机号码" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.permission" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  ref="upload" @click="cancelEditForm()">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return{
      tableData: [],
      dialogFormVisible: false,
      dialogVisible: false,
      ruleForm: {
        id: '',
        username: '',
        nickname: '',
        password:'',
        phone: '',
        enable:'',
        permission:''
      },
      formLabelWidth: '120px'
    }
  },
  methods:{
    loadUserList() {
      let url = 'http://localhost:9580/users/admin/getUserList';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
      });
    },
    updateEnable(user) {
      let enableStatus = [
        {'url': '/disable', text: '禁用'},
        {'url': '/enable', text: '启用'}
      ];
      let url = 'http://localhost:9580/users/'
          + user.id + enableStatus[user.enable].url;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: enableStatus[user.enable].text + '成功！',
            type: 'success'
          });
          this.loadUserList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadUserList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    openEditDialog(user) {
      let url = 'http://localhost:9580/users/admin/' + user.id+'/getAdminUser';

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.ruleForm = responseBody.data;
          this.loadUserList();
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadUserList();
            }

          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://localhost:9580/users/admin/' + this.ruleForm.id + '/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.ruleForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url,formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改成功',
            type: 'success'
          });
          this.loadUserList();
          this.dialogFormVisible = false;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadUserList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    cancelEditForm() {
      this.dialogFormVisible = false;
    },
    openDeleteConfirm(user) {
      let message = '此操作将永久删除【' + user.username + '】，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(user);
      }).catch(() => {
      });

    },
    handleDelete(user) {
      let url = 'http://localhost:9580/users/admin/' + user.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除用户成功！',
            type: 'success'
          });
          this.loadUserList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadUserList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
  },
  mounted() {
    this.loadUserList();
  }
}
</script>

<style scoped>

</style>